/**
 * 格式化时间
 * @param {Object} timestamp 时间戳
 * @param {Object} vminimum 模式 1:2019-08-22 ;2:2019-08-22 上午;3:2019-08-22 18:00;4:2019.08.22;
 * @param {Object} stime 上午,下午 1:上午,2:下午
 */
function DateFormat(timestamp, vminimum, stime) {
	//时间戳格式化
	let date = new Date(timestamp + 8 * 3600 * 1000); // 增加8小时
	switch (vminimum) {
		//表示按天
		case '1':
			return date.toJSON().substr(0, 10).replace('T', ' ');
		case '2':
			//表示半天
			return date.toJSON().substr(0, 10).replace('T', ' ').concat(stime == 1 ? ' 上午' : ' 下午');
			//表示小时
		case '3':
			return date.toJSON().substr(0, 16).replace('T', ' ');
			//都不是就取提交日期
		default:
			let time = new Date(timestamp);
			let format = ''
			let today = new Date();
			let yestday = new Date(today - 24 * 3600 * 1000);
			if (time.setHours(0, 0, 0, 0) == today.setHours(0, 0, 0, 0)) {
				format = date.toJSON().substring(11, 16).replace('T', ' ').replace(/-/g, '.')
			} else if (time.setHours(0, 0, 0, 0) == yestday.setHours(0, 0, 0, 0)) {
				format = '昨天'
			} else {
				format = date.toJSON().substr(0, 10).replace('T', ' ').replace(/-/g, '.')
			}
			return format
	}
}


function getFormatTime(timestamp) {
	//时间戳格式化
	let date = new Date(timestamp + 8 * 3600 * 1000); // 增加8小时
	return date.toJSON().substring(11, 16).replace('T', ' ').replace(/-/g, '.')
}

function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};

function formateDate(datetime, type) {
	var year = datetime.getFullYear(),
		month = ("0" + (datetime.getMonth() + 1)).slice(-2),
		date = ("0" + datetime.getDate()).slice(-2),
		hour = ("0" + datetime.getHours()).slice(-2),
		minute = ("0" + datetime.getMinutes()).slice(-2),
		second = ("0" + datetime.getSeconds()).slice(-2);
	let ctime = ''
	if (hour >= 6 && hour < 8) {
		ctime = '早上'
	} else if (hour >= 8 && hour < 11) {
		ctime = '上午'
	} else if (hour >= 11 && hour < 13) {
		ctime = '中午'
	} else if (hour >= 13 && hour < 18) {
		ctime = '下午'
	} else {
		ctime = '晚上'
	}

	if (type === "Y-M-D h:min:s") {
		var result = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
	} else if (type === "Y-M-D h:min") {
		var result = year + "-" + month + "-" + date + " " + hour + ":" + minute;
	} else if (type === "Y-M-D h:min A") {
		var result = year + "-" + month + "-" + date + " " + hour + ":" + minute + " "+ctime;
	} else if (type === "Y-M-D") {
		var result = year + "-" + month + "-" + date;
	} else if (type === "Y-M") {
		var result = year + "-" + month;
	} else if (type === "h:min:s") {
		var result = hour + ":" + minute + ":" + second;
	} else if (type === "h:min") {
		var result = hour + ":" + minute;
	}else if(type==="YMDh"){
		var result = year + month + date  + hour;
	}
	return result;
}

function getWeek(week) {
	switch (week) {
		case 0:
			return '星期日';
		case 1:
			return '星期一';
		case 2:
			return '星期二';
		case 3:
			return '星期三';
		case 4:
			return '星期四';
		case 5:
			return '星期五';
		case 6:
			return '星期六';
	}
}

module.exports = {
	formatTime: formatTime,
	formatLocation: formatLocation,
	dateUtils: dateUtils,
	getWeek: getWeek,
	getFormatTime: getFormatTime,
	formateDate: formateDate,
	DateFormat:DateFormat
}
